<!--
 * @Description: 地图
 * @Author: jiansheng
 * @Date: 2024-05-16 09:08:45

 cesium API 文档
 http://cesium.xin/cesium/cn/Documentation1.62/
-->
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
	<meta name="viewport"
		content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
	<title>倾斜模型</title>
	<link rel="stylesheet" href="./Cesium/Widgets/widgets.css">
	<script src="./Cesium/jquery.min.js"></script>
	<script type="text/javascript" src="./Cesium/Cesium.js"></script>
</head>

<body>
	<div id="cesiumContainer"></div>
	<script>
		let viewer = undefined
		let baseUrl = 'http://172.17.1.100:3000'
		let replaceUrl = '/lgs/s3m/ningzhuang/rest/realspace/datas'
		let tdkey = '691d3548db4a32abaedb6c077c33b8a3'
		let param = {}
		let subdomains = ['http://172.17.1.100:3000']
		let DEF_OPTION = {
			maxVisibleAltitude: 8000,
			clearMemoryImmediately: true,
			visibleDistanceMax: 4000,
			lodRangeScale: 0.7,
			brightness: 1.15
		}

		function initViewer() {
			viewer = new Cesium.Viewer('cesiumContainer');
			viewer.camera.setView({
				destination: Cesium.Cartesian3.fromDegrees(116.853762, 35.711586, 500),
				orientation: {
					// 相机绕垂直轴的旋转角度，单位为弧度。heading 规定了相机的水平方向，0 表示相机朝向正北方向，以顺时针方向旋转为正值，以弧度为单位
					heading: Cesium.Math.toRadians(0),
					// 相机绕横轴的旋转角度，单位为弧度。pitch 规定了相机的俯仰角度，正值表示相机朝向地面，负值表示相机朝向天空，以弧度为单位
					pitch: Cesium.Math.toRadians(-90),
					// 相机绕相机自身轴的旋转角度，单位为弧度。roll 规定了相机的横滚角度，正值表示相机顺时针旋转，负值表示相机逆时针旋转，以弧度为单位
					roll: Cesium.Math.toRadians(0)
				}
			});

			viewer._cesiumWidget._creditContainer.style.display = "none";


			setTimeout(() => {
				addGeojsonLayer()
			}, 4000)
			// setTimeout(() => {
			// 	viewer.camera.flyTo({
			// 		destination: Cesium.Cartesian3.fromDegrees(116.853762, 35.711586, 1000),
			// 		duration: 3,
			// 		complete() {
			// 			// addTMap(viewer, 'img')
			// 		}
			// 	});
			// }, 3000)
		}

		/** 加载 geojson 图层 */
		// https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=370921
		// http://172.17.1.100:8080/geoserver/ningzhuang/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=ningzhuang%3AYL&maxFeatures=50&outputFormat=application%2Fjson
		// http://172.17.1.100:8080/geoserver/ningzhuang/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=ningzhuang%3Anz84&maxFeatures=50&outputFormat=application%2Fjson
		function addGeojsonLayer() {
			console.log('--------')
			Cesium.GeoJsonDataSource.load('http://127.0.0.1:5502/wgs84.geojson').then(dataSource => {
				viewer.dataSources.add(dataSource);
				// var entities = dataSource.entities.values;

				// for (var i = 0; i < entities.length; i++) {
				// 	var entity = entities[i];
				// 	entity.polyline.width = 5;
				// 	entity.polyline.clampToGround = true;
				// 	entity.polyline.material = new Cesium.PolylineOutlineMaterialProperty({
				// 		color: Cesium.Color.RED,
				// 		outlineWidth: 2,
				// 		outlineColor: Cesium.Color.BLACK,
				// 	})
				// }
			})
		}


		/** 加载3D模型图 */
		function getLayerMedaData() {
			const url = baseUrl + replaceUrl
			$.get(url, {}, function (res) {
				if (res && res.length > 0) {
					res.forEach((item) => {
						addSceneS3mLayer({
							name: item.name,
							url: baseUrl + item.path + '/config.scp',
							param: param
						})
					})
				}
			});
		}
		function addSceneS3mLayer(options) {
			const scene = viewer.scene
			// const widget = viewer.cesiumWidget
			// const scmLayer = scene.layers.find(options.name)

			// const scpParam = {
			// 	name: options.name,
			// 	cullEnabled: false,
			// 	autoSetView: true
			// }

			const groPromise = scene.addS3MTilesLayerByScp(options.url, {})
			// Cesium.when(groPromise, (layer) => {
			// 	let layerParam = DEF_OPTION
			// 	console.log('新增三维图层参数', layerParam)
			// 	// for (const key in layerParam) {
			// 	// 	layer[key] = layerParam[key]
			// 	// }
			// 	console.log(`新增三维图层${options.name}`)
			// }, function (e) {
			// 	if (widget._showRenderLoopErrors) {
			// 		console.error('三维渲染失败', e)
			// 	}
			// })
		}
		function test() {
			setTimeout(() => {
				const cartesian = Cesium.Cartesian3.fromDegrees(116.778050, 35.551938)
				const updatedCartesian = viewer.scene.clampToHeight(cartesian)
				if (updatedCartesian) {
					const cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(
						updatedCartesian
					)
					const lon = Cesium.Math.toDegrees(cartographic.longitude)
					const lat = Cesium.Math.toDegrees(cartographic.latitude)
					const height = cartographic.height
					console.log('111111111', lon, lat, height)
				} else {
					console.log('qqqqqqqqqqqqqq')
				}
			}, 10000)
		}
		window.onload = function () {
			initViewer()
			getLayerMedaData()
		}
	</script>
</body>

</html>